/**
 * 图屋 App
 * https://github.com/facebook/react-native
 * @author lily
 */

import React, { Component } from 'react'
import {Root} from 'native-base'
import { DeviceEventEmitter, Platform, Alert, Linking } from 'react-native'
import './src/libs/global'
import './src/libs/storage'
import { Router, Stack, Scene } from 'react-native-router-flux'
import Home from './src/views/home'
import Login from './src/views/login'
import SettingsIndex from './src/views/settings/index/index'
import Information from './src/views/settings/information/index'
import BindPhone from './src/views/settings/bindPhone/index'
import Article from './src/views/article/index/index'
import ArticleDetail from './src/views/article/detail/index'
import Recruit from './src/views/recruit/index'
import RecruitDetail from './src/views/detail/recruit/index'
import IncomeDetail from './src/views/detail/income/index'
import Withdraw from './src/views/trade/withdraw/index'
import WithdrawRecord from './src/views/trade/withdraw/record'
import TabIcon from './src/components/tabIcon'
import HeaderC from './src/components/header/commonHeader'
import OneInput from './src/views/edit/oneInput'
import Search from './src/views/search'
import ErrorC from './src/views/error'
import Guide from './src/views/guide'
import Qrcode from './src/views/qrcode'
import Useragreement from './src/views/settings/useragreement'
import commonStyle from './src/libs/commonStyle'
import * as WeChat from 'react-native-wechat'
import wechatConfig from './src/config/wechat'
import { Provider } from 'react-redux'
import configureStore from './src/redux/store'
import { PersistGate } from 'redux-persist/es/integration/react'
import MyWeb from './src/views/article/detail/webview'

import {
  isFirstTime,
  isRolledBack,
  packageVersion,
  currentVersion,
  checkUpdate,
  downloadUpdate,
  switchVersion,
  switchVersionLater,
  markSuccess,
} from 'react-native-update'
import _updateConfig from './update.json'
const {appKey} = _updateConfig[Platform.OS]

const { persistor, store } = configureStore()

export default class RootApp extends Component {
	constructor(props) {
		super(props)
	}
	componentDidMount() {
		this.checkUpdate()
		WeChat.registerApp(wechatConfig.appid)
	}
	doUpdate = info => {
		downloadUpdate(info).then(hash => {
			switchVersionLater(hash)
// 		  Alert.alert('提示', '下载完毕,是否重启应用?', [
// 			{text: '是', onPress: ()=>{switchVersion(hash);}},
// 			{text: '否',},
// 			{text: '下次启动时', onPress: ()=>{switchVersionLater(hash);}},
// 		  ])
		}).catch(err => { 
		  // Alert.alert('提示', '更新失败.');
		})
	}
	
	checkUpdate = () => {
		checkUpdate(appKey).then(info => {
			if (info.expired) {
// 				Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [
// 				  {text: '确定', onPress: ()=>{info.downloadUrl && Linking.openURL(info.downloadUrl)}},
// 				])
			} else if (info.upToDate) {
				// Alert.alert('提示', '您的应用版本已是最新.')
			} else {
				this.doUpdate(info)
// 				Alert.alert('提示', '检查到新的版本'+info.name+',是否下载?\n'+ info.description, [
// 				  {text: '是', onPress: ()=>{this.doUpdate(info)}},
// 				  {text: '否',},
// 				])
			  }
		}).catch(err => { 
			// Alert.alert('提示', '更新失败.');
		})
	 }

	render() {
		return (
		<Root>
			<Provider store={store}>
				<PersistGate persistor={persistor}>
					<Router sceneStyle={{ backgroundColor: '#f5f5f5' }}>
						<Stack key="root">
							<Scene
								key="tabbar"
								initial
								tabs
								tabBarPosition="bottom"
								hideNavBar
								showLabel={false}
								tabBarStyle={commonStyle.tabBarStyle}
								// tabBarSelectedItemStyle={styles.tabBarSelectedItemStyle}
								// titleStyle={commonStyle.titleStyle}
							>
								<Scene
									key="home"
									settings
									navBar={HeaderC}
									component={Home}
									onEnter={()=>{
										DeviceEventEmitter.emit('onenter.home')
									}}
									icon={TabIcon}
								/>
								<Scene
									key="article"
									search
									// hideNavBar
									navBar={HeaderC}
									component={Article}
									icon={TabIcon}
								/>
								<Scene
									key="recruit"
									// hideNavBar
									navBar={HeaderC}
									component={Recruit}
									icon={TabIcon}
								/>
							</Scene>
							<Scene
								key="login"
								hideNavBar
								title="登录"
								component={Login}
							/>
							<Scene
								key="settingsIndex"
								title="设置"
								hideNavBar
								component={SettingsIndex}
							/>
							<Scene
								key="information"
								title="我的资料"
								hideNavBar
								component={Information}
							/>
							<Scene
								key="bindPhone"
								title="绑定手机号"
								hideNavBar
								component={BindPhone}
							/>
							<Scene
								key="oneInput"
								hideNavBar
								title="编辑信息"
								placeholder="请输入师傅收徒码"
								component={OneInput}
							/>
							<Scene
								key="articleDetail"
								title="文章详情"
								hideNavBar
								component={ArticleDetail}
							/>
							<Scene
								key="adetail"
								title="文章详情"
								hideNavBar
								component={MyWeb}
							/>
							<Scene
								key="recruitDetail"
								title="我的徒弟"
								hideNavBar
								component={RecruitDetail}
							/>
							<Scene
								key="incomeDetail"
								title="收入详情"
								hideNavBar
								component={IncomeDetail}
							/>
							<Scene
								key="withdraw"
								title="我要提现"
								hideNavBar
								component={Withdraw}
							/>
							<Scene
								key="withdrawRecord"
								title="提现记录"
								hideNavBar
								component={WithdrawRecord}
							/>
							<Scene
								key="guide"
								title="赚钱攻略"
								hideNavBar
								component={Guide}
							/>
							<Scene
								key="useragreement"
								title="用户协议"
								hideNavBar
								component={Useragreement}
							/>
							
							<Scene key="search" hideNavBar component={Search} />
							<Scene key="error" hideNavBar component={ErrorC} />
							<Scene key="qrcode" hideNavBar component={Qrcode} />
						</Stack>
					</Router>
				</PersistGate>
			</Provider>
			</Root>
		)
	}
}
